﻿@using System.Linq
@using Microsoft.AspNetCore.Http.Extensions;
@using Dignite.Abp.AspNetCore.Mvc.UI.Theme.Pure.Themes.Pure.Components.Toolbar.LocaleSwitch;
@using System.Globalization
@model LocaleSwitchViewComponentModel
@if (Model.AvailableCultures.Any()&&Model.AvailableCultures.Count>1)
{
    var currentCulture = Model.AvailableCultures.FirstOrDefault(l => l.Name.Equals(Model.CurrentCultureName, System.StringComparison.OrdinalIgnoreCase))??
        Model.AvailableCultures.FirstOrDefault(l => l.Name.Equals(Model.DefaultCultureName, System.StringComparison.OrdinalIgnoreCase));
    <div class="dropdown">
        <a class="btn" href="#" id="Menu_CultureSwitch" data-bs-toggle="dropdown" aria-haspopup="true" aria-expanded="false" title="@currentCulture?.NativeName">
            <i class="fa fa-globe" aria-hidden="true"></i>
            <small>
                @currentCulture?.NativeName
            </small>
        </a>
        <div class="dropdown-menu dropdown-menu-end" aria-labelledby="Menu_CultureSwitch">
            @foreach (var culture in Model.AvailableCultures)
            {
                if (Model.CurrentCultureName == culture.Name)
                {
                    <span class="dropdown-item active">
                        @culture.NativeName
                    </span>
                }
                else
                {
                    var uiCultureName = culture.CultureTypes.HasFlag(CultureTypes.NeutralCultures) ? culture.Name : culture.Parent.Name;
                    string url=$"Abp/Languages/Switch?culture={culture.Name}&uiCulture={uiCultureName}";
					string returnUrl = string.Empty;
                    if (Model.IsMatchingRoute)
                    {
                        returnUrl = Model.InsertOrReplaceCultureParameter(Context, culture.Name);
                        returnUrl = returnUrl + Context.Request.QueryString.Value;
                    }
                    else
                    {
                        returnUrl = System.Net.WebUtility.UrlEncode(Context.Request.GetEncodedPathAndQuery());
                    }
                    url += $"&returnUrl={returnUrl}";
                    <a class="dropdown-item" href="~/@url">
                        @culture.NativeName
                    </a>
                }
            }
        </div>
    </div>
}
